<template>
  <div class="user-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>用户管理</span>
        <el-button type="primary" @click="handleAdd" style="float: right;">新增用户</el-button>
      </div>
      
      <el-table :data="users" border style="width: 100%">
        <el-table-column prop="username" label="用户名" width="180"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="phone" label="电话"></el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
            <el-button size="mini" type="warning" @click="handleResetPassword(scope.row)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
    <!-- 新增/编辑弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </span>
    </el-dialog>
    
    <!-- 重置密码弹窗 -->
    <el-dialog title="重置密码" :visible.sync="resetDialogVisible" width="30%">
      <el-form :model="resetForm" :rules="resetRules" ref="resetForm">
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="resetForm.newPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="resetForm.confirmPassword" type="password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitResetForm">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      users: [],
      dialogVisible: false,
      resetDialogVisible: false,
      dialogTitle: '',
      currentUserId: null,
      form: {
        userId: null,
        username: '',
        email: '',
        phone: ''
      },
      resetForm: {
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }]
      },
      resetRules: {
        newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
        confirmPassword: [{ required: true, message: '请确认密码', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchUsers();
  },
  methods: {
    fetchUsers() {
      // 调用API获取用户列表
      console.log('获取用户列表');
      // 模拟数据
      this.users = [
        {
          userId: 1,
          username: '测试用户',
          email: 'test@example.com',
          phone: '13800138000'
        }
      ];
    },
    handleAdd() {
      this.dialogTitle = '新增用户';
      this.resetForm();
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.dialogTitle = '编辑用户';
      this.form = Object.assign({}, row);
      this.dialogVisible = true;
    },
    handleDelete(row) {
      this.$confirm('确认删除该用户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用API删除用户
        console.log('删除用户:', row.userId);
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.fetchUsers();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });        
      });
    },
    handleResetPassword(row) {
      this.currentUserId = row.userId;
      this.resetForm = {
        newPassword: '',
        confirmPassword: ''
      };
      this.resetDialogVisible = true;
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 调用API保存用户
          console.log('保存用户:', this.form);
          this.$message({
            type: 'success',
            message: '保存成功!'
          });
          this.dialogVisible = false;
          this.fetchUsers();
        }
      });
    },
    submitResetForm() {
      this.$refs.resetForm.validate(valid => {
        if (valid) {
          if (this.resetForm.newPassword !== this.resetForm.confirmPassword) {
            this.$message.error('两次输入的密码不一致');
            return;
          }
          // 调用API重置密码
          console.log('重置密码:', this.currentUserId, this.resetForm.newPassword);
          this.$message({
            type: 'success',
            message: '密码重置成功!'
          });
          this.resetDialogVisible = false;
        }
      });
    },
    resetForm() {
      this.form = {
        userId: null,
        username: '',
        email: '',
        phone: ''
      };
    }
  }
}
</script>

<style scoped>
.user-container {
  padding: 20px;
}
</style>